<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    class="flex"
                    :disabled="baTable.form.operate=='view'"
                >
                <FormItem :label="t('xCorpbank.CORP')" type="string" v-model="baTable.form.items!.CORP" prop="CORP" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.CORP') }),disabled:true }" />
                <div class="el-form-item"></div>
                <FormItem :label="t('xCorpbank.BANKNAME')" type="string" v-model="baTable.form.items!.BANKNAME" prop="BANKNAME" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.BANKNAME') }) }" />
                <FormItem :label="t('xCorpbank.CORPNAME')" type="string" v-model="baTable.form.items!.CORPNAME" prop="CORPNAME" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.CORPNAME') }) }" />
                <FormItem :label="t('xCorpbank.BANKACCOUNT')" type="string" v-model="baTable.form.items!.BANKACCOUNT" prop="BANKACCOUNT" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.BANKACCOUNT') }) }" />
                <FormItem :label="t('xCorpbank.ZZSFLAG')" 
                v-model="baTable.form.items!.ZZSFLAG" 
                prop="ZZSFLAG" 
                type="select"
                :data="{ content: { '增票': '增票', '普票': '普票' } }"
                :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.ZZSFLAG') }) }" 
                />
                <FormItem 
                :label="t('xCorpbank.MONEYTYPE')" 
                v-model="baTable.form.items!.MONEYTYPE" 
                prop="MONEYTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'MONEYTYPE',
                    field: 'CURNAME',
                    'remote-url': codelib + 'curr',
                    placeholder: t('Click Select'),
                }"
                />
                <FormItem :label="t('xCorpbank.TEL')" type="string" v-model="baTable.form.items!.TEL" prop="TEL" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.TEL') }) }" />
                <FormItem style="width:100%" :label="t('xCorpbank.ADDR')" type="string" v-model="baTable.form.items!.ADDR" prop="ADDR" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.ADDR') }) }" />
                <FormItem style="width:100%" :label="t('xCorpbank.REMARK')" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('xCorpbank.REMARK') }) }" />
                <!-- <FormItem :label="t('xCorpbank.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: '当前人' }" /> -->
                <!-- <FormItem :label="t('xCorpbank.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前日期' }" /> -->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate!=='view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { useRoute } from 'vue-router'
import { codelib , corplib} from '/@/api/controllerUrls'
import { getSelectData } from '/@/api/common'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    BANKNAME: [buildValidatorData({ name: 'required', title: t('xCorpbank.BANKNAME') })],
    BANKACCOUNT: [buildValidatorData({ name: 'required', title: t('xCorpbank.BANKACCOUNT') })],
    CORPNAME:[buildValidatorData({ name: 'required', title: t('xCorpbank.CORPNAME') })],
    MONEYTYPE: [buildValidatorData({ name: 'required', title: t('xCorpbank.MONEYTYPE') })],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xCorpbank.OPTIME') })})],
})

const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value
        baTable.form.items!.VALIDORNOT = '1'
        getCurrentCorp(companyName.value)
    }
})



const getCurrentCorp = (name:string)=>{
    let url = corplib + 'index'
    getSelectData(url,name,{
        initValue:name,
    }).then(res=>{
        let item = res.data.list[0] || {}
        baTable.form.items!.CORPNAME = item.CORPNAME
    })
}



const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}

</script>

<style scoped lang="scss"></style>
